package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.rfid.base.base.BaseAdapter;
import com.rfid.base.base.BaseListFragment;
import com.rfid.base.base.BasePresenter;
import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.presenter.ReadTagPresenter;
import com.rfid.emergencywarehouse.reader.MDividerItemDecoration;
import com.rfid.emergencywarehouse.util.DialogUtil;
import com.rfid.emergencywarehouse.view.IScanView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Optional;

public abstract class BaseScanListFragment<T extends BaseAdapter,Y extends BarcodeInfo,G extends ReadTagPresenter> extends BaseListFragment<T,BarcodeInfo,G> implements IScanView, CompoundButton.OnCheckedChangeListener {

    TextView mTip;

    View mRoot;

    CheckBox mRfid;

    List<String> mBarcodes = new ArrayList<>();
    List<BarcodeInfo> mSelects = new ArrayList<>();

    @Override
    protected int getRecyclerViewId() {
        return R.id.recyclerView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTip = view.findViewById(R.id.scan_tip);
        mRoot = view.findViewById(R.id.root);
        mRfid = view.findViewById(getScanRfidCheckBoxId());
        mRfid.setOnCheckedChangeListener(this);
        MDividerItemDecoration itemDecoration = new MDividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);
        freshTip();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mBarcodes.clear();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mPresenter.stopScanRfid();
    }

    protected abstract int getScanRfidCheckBoxId();

    protected abstract EditText getBarcodeEdit();

    @Override
    public void onClick(BarcodeInfo info) {
        EditText editText = getBarcodeEdit();


        System.out.println("onclick " + (editText != null));
        if (editText != null) {
            editText.setText(info.getBarcode());
        }
    }

    @Override
    public void onBarcode(String rfid, String barcode) {
        if (mBarcodes.contains(barcode)) {
            return;
        }
        requestBarcodeInfo(barcode);
//        mData.add();
//        mAdapter.notifyDataSetChanged();
//        freshTip();
    }

    protected void requestBarcodeInfo(String barcode) {
        mPresenter.queryBarcodeInfo(barcode);
    }

    protected void showSuccess(String msg) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (mRoot != null) {
            DialogUtil.showSuccessDialog(context,mRoot,msg);
        }
    }

    protected void showTip(String msg) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (mRoot != null) {
            DialogUtil.showTipDialog(context,mRoot,msg);
        }
    }

    @Override
    public void onBarcodeQuery(BarcodeInfo info,boolean success) {
        if (mBarcodes.contains(info.getBarcode())) {
            return;
        }
        mBarcodes.add(info.getBarcode());
        boolean add = needAdd(info);
        if (add) {
            mData.add(info);
        } else {
            return;
        }
        boolean select = needSelect(info);
        if (select) {
            info.setSelect(true);
            mSelects.add(info);
        }
        mAdapter.notifyDataSetChanged();
        freshTip();
    }

    protected boolean needAdd(BarcodeInfo info) {
        return true;
    }

    protected  boolean needSelect(BarcodeInfo info) {
        return true;
    }

    @Optional
    @OnClick(R.id.cancel)
    public void cancel(View v) {
        getActivity().onBackPressed();
    }

    protected void freshTip() {
        mTip.setText(getScanTip(mData.size()));
    }

    protected  String getScanTip(int size) {
        return String.format("已扫描数据%s条，已选中%s条",size,mSelects.size());
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            onStartScan();
            mBarcodes.clear();
            mData.clear();
            mAdapter.notifyDataSetChanged();
            mPresenter.startScanRfid();
        } else {
            mPresenter.stopScanRfid();
        }
    }

    protected void onStartScan() {
        mSelects.clear();
        freshTip();
    }

    @Override
    public void onSelect(BarcodeInfo info, boolean select) {
        System.out.println("onSelect " + info.getBarcode() + ",select = " + select + ",size = " + mSelects.size());
        if (select) {
            if (!mSelects.contains(info)) {
                mSelects.add(info);
            }
        } else {
            mSelects.remove(info);
        }
        EditText editText = getBarcodeEdit();
        if (editText != null) {
            if (mSelects.size() > 0) {
                editText.setText("");
                editText.setEnabled(false);
            } else {
                editText.setEnabled(true);
            }
        }
        freshTip();
    }

    protected List<String> getBarcodes() {
        EditText editText = getBarcodeEdit();
        String barcode = null;
        if (editText!= null) {
            barcode = editText.getText().toString();
        }
        System.out.println("size = " + mSelects.size());
        if (TextUtils.isEmpty(barcode) && mSelects.size() == 0) {
            if (editText != null) {
                AnimatorHelper.shake(getContext(), editText);
            }
            return null;
        }
        List<String> barcodes = new ArrayList<>();
        if (!TextUtils.isEmpty(barcode)) {
            barcodes.add(barcode);
        } else {
            for (BarcodeInfo info : mSelects) {
                barcodes.add(info.getBarcode());
            }
        }
        return barcodes;
    }

    protected void clearSelectData() {
        for (BarcodeInfo info : mSelects) {
            mData.remove(info);
        }
        mSelects.clear();
        freshTip();
        mAdapter.notifyDataSetChanged();
    }
}
